package cn.xcstudenth.myapplication.texviewutils;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;

import org.xml.sax.XMLReader;

import java.util.Locale;

import cn.xcstudenth.myapplication.AppComplication;
import cn.xcstudenth.myapplication.ConstantUtils;
import cn.xcstudenth.myapplication.ImageAmplifyActivity;

/**
 * Created by 赵航 on 2020/6/5.
 * WeChat: zhao--hang--
 */
public class HtmlTagHandler implements Html.TagHandler {

    private static final String IMG = "img";
    public HtmlTagHandler() {
    }

    @Override
    public void handleTag(boolean b, String tag, Editable editable, XMLReader xmlReader) {

        // 处理标签<img>          
        if (tag.toLowerCase(Locale.getDefault()).equals(IMG)) {
            // 获取长度              
            int len = editable.length();
            // 获取图片地址
            ImageSpan[] images = editable.getSpans(len - 1, len, ImageSpan.class);
            String      imgURL = images[0].getSource();
            // 使图片可点击并监听点击事件
            editable.setSpan(new ClickableImage(AppComplication.mContext, imgURL), len - 1, len,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        //自定义标签，点击进行跳转
        //        if (tag.toLowerCase().equals("game")) {
        //            if (b) {
        //                startGame(tag, editable, xmlReader);
        //            } else {
        //                endGame(tag, editable, xmlReader);
        //            }
        //        }
    }

    private class ClickableImage extends ClickableSpan {
        private String  url;
        private Context context;

        public ClickableImage(Context context, String url) {
            this.context = context;
            this.url = url;
        }

        @Override
        public void onClick(View widget) {
            // 进行图片点击之后的处理
            if (!TextUtils.isEmpty(url)) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(context, ImageAmplifyActivity.class);
                intent.putExtra(ConstantUtils.IMG, url);
                context.startActivity(intent);
            }
        }
    }



    private int startIndex = 0;
    private int stopIndex = 0;
    private void startGame(String tag, Editable output, XMLReader xmlReader) {
        startIndex = output.length();
    }

    private void endGame(String tag, Editable output, XMLReader xmlReader) {
        stopIndex = output.length();
        output.setSpan(new GameSpan(), startIndex, stopIndex,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }


    private class GameSpan extends ClickableSpan implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // 跳转某页面
        }
    }
}
